<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .search_input {
            cursor: pointer;
        }
    </style>
</head>

<body>
    <input type="search" class="search_input"> <button class="search">搜索</button>
    <div>

        <button class="prev">上一页</button>
        <button class="play">播放</button>
        <button class="next">下一页</button>
    </div>
    <table border="1">
        <thead>
            <th>歌名</th>
            <th>歌手</th>
            <th>专辑名称</th>
            <th>时长</th>
        </thead>
        <tbody></tbody>


    </table>

    <script>
        function _ajaxTool(method, url, data, cb) {
            let xhr = new XMLHttpRequest();
            if (method == "GET") {
                //处理get
                //对传进来的data(js对象)参数进行处理
                let a = "";
                for (let key in data) {
                    a += `${key}=${data[key]}&`;
                }
                let newUrl = `${url}?${a.slice(0, -1)}`;
                console.log(newUrl);
                xhr.open(method, newUrl);
                xhr.send();
            } else {
                //处理post
                //对data操作形成新的字符串
                let postData = "";
                xhr.open(method, url);
                xhr.setRequestHeader(
                    "Content-type",
                    "application/x-www-form-urlencoded"
                );
                for (let key in data) {
                    postData += `${key}=${data[key]}&`;
                }
                postData = postData.slice(0, -1);
                xhr.send(postData);
            }
            xhr.addEventListener("readystatechange", function() {
                if (xhr.status == 200) {
                    if (xhr.readyState == 4) {
                        cb(JSON.parse(xhr.responseText));
                    }
                }
            });
        }

        let url = `http://netease.0melon0.cn/search`;
        let data = {
            // keywords: " ",
            limit: 10,
            offset: 0,
        };
        let prev = document.querySelector('.prev');
        prev.addEventListener('click', function() {
            data.offset -= data.limit;
            if (data.offset < 0) {
                data.offset = 0;
            }
            _ajaxTool("GET", url, data, render);
        })
        let next = document.querySelector('.next');

        next.addEventListener('click', function() {
            data.offset += data.limit;
            _ajaxTool("GET", url, data, render);
        })


        // _ajaxTool("GET", url, data, render);

        let tbody = document.querySelector('tbody');

        function render(data) {
            console.log(data);
            let s = data.result.songs[0];
            console.log(data.result.songs[0].name, data.result.songs[0].artists[0].name, data.result.songs[0].album.name, data.result.songs[0].duration);
            // data.result.songs[0].name
            // data.result.songs[0].artists[0].name
            // data.result.songs[0].album.name
            // data.result.songs[0].duration
            tbody.innerHTML = ''; // 清空数据
            data.result.songs.forEach(function(ele) {
                tbody.innerHTML += `
                <tr>
            <td>${ele.name}</td>
            <td>${ele.artists[0].name}</td>
            <td>${ele.album.name}</td>
            <td>${sj(ele.duration)}</td>
        </tr>
                `
            })
        }

        function sj(duration) {
            let m = parseInt((duration / 1000 / 60) % 60); // 分钟
            let s = parseInt((duration / 1000) % 60) // 秒
            s = s < 10 ? '0' + s : s; // 补零
            return `${m}:${s}`
        }
        let search = document.querySelector('.search');
        let ipt = document.querySelector('input');

        search.addEventListener('click', function() {
            if (ipt.value != '') {
                search = ipt.value;
                data.keywords = search
            }
            _ajaxTool("GET", url, data, render);
        })
    </script>
</body>

</html>